package com.vehicle.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.mapper.BtypeMapper;
import com.vehicle.mapper.CarMapper;
import com.vehicle.mapper.RepairMapper;
import com.vehicle.mapper.RepairRecordMapper;
import com.vehicle.pojo.Btype;
import com.vehicle.pojo.Car;
import com.vehicle.pojo.Repair;
import com.vehicle.pojo.RepairRecord;
import com.vehicle.service.RepairRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RepairRecordServiceImpl implements RepairRecordService {

    @Autowired
    private RepairRecordMapper repairRecordMapper;

    @Autowired
    private BtypeMapper btypeMapper;

    @Autowired
    private CarMapper carMapper;

    @Autowired
    private RepairMapper repairMapper;

    @Override
    public DataGridView findAllRepairRecord(Integer carid, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        DataGridView dataGridView = new DataGridView();
        List<RepairRecord> repairRecord = repairRecordMapper.findAllRepairRecord(carid);
        PageInfo pageInfo = new PageInfo(repairRecord);
        dataGridView.setCount(pageInfo.getTotal());
        dataGridView.setData(repairRecord);
        return dataGridView;
    }

    @Override
    public List<Btype> findAllRepairBtype() {
        return btypeMapper.findAllBtypeByRepair();
    }

    @Override
    public ResultDto addRepairRecord(RepairRecord repairRecord) {
        ResultDto resultDto = new ResultDto();
        try {
            repairRecordMapper.addRepairRecord(repairRecord);
            carMapper.updateCarableById(repairRecord.getCarid(),3);
            resultDto.setCode(1001);
            resultDto.setMsg("登记维修信息成功！");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("登记维修信息失败!");
        }
        return resultDto;
    }

    @Override
    public List<Car> findAllCar() {
        return carMapper.selectCarByLazy();
    }

    @Override
    public List<Repair> findAllRepair() {
        return repairMapper.findAllRepair();
    }

    @Override
    public ResultDto delManyRepairRecord(String idArray,String carid) {
        ResultDto resultDto = new ResultDto();
        try {
            repairRecordMapper.delManyRepairRecord(idArray);
            carMapper.updateCarableInId(carid,2);
            resultDto.setMsg("删除成功");
            resultDto.setCode(1001);
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setMsg("删除失败");
            resultDto.setCode(-1001);
        }
        return resultDto;
    }

    @Override
    public ResultDto delRepairRecord(Integer repairid,Integer carid) {
        ResultDto resultDto = new ResultDto();
        try {
            repairRecordMapper.delRepairRecord(repairid);
            carMapper.updateCarableById(carid,2);
            resultDto.setCode(1001);
            resultDto.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("删除失败");
        }
        return resultDto;
    }

    @Override
    public RepairRecord findRecordById(Integer repairid) {
            RepairRecord repairRecord = repairRecordMapper.findRepairRecordById(repairid);
            return repairRecord;
    }

    @Override
    public ResultDto updateRecord(RepairRecord repairRecord) {
        ResultDto resultDto = new ResultDto();
        try {
            repairRecordMapper.updateRepairRecord(repairRecord);
            resultDto.setCode(1001);
            resultDto.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
            resultDto.setMsg("修改失败");
        }
        return resultDto;
    }

    @Override
    public ResultDto bringCar(RepairRecord repairRecord) {
        ResultDto resultDto = new ResultDto();
        try {
            repairRecordMapper.bringCar(repairRecord);
            carMapper.updateCarableById(repairRecord.getCarid(),2);
            resultDto.setCode(1001);
        }catch (Exception e){
            e.printStackTrace();
            resultDto.setCode(-1001);
        }
        return resultDto;
    }
}
